/**
 * @param {number[]} pushed
 * @param {number[]} popped
 * @return {boolean}
 */
const validateStackSequences = function (pushed, popped) {
  let stack = []
  let j = 0
  for (let v of pushed) {
    stack.push(v)
    while (stack[stack.length - 1] === popped[j] && stack.length > 0) {
      stack.pop()
      j++
    }
  }
  return !stack.length
};

console.log(validateStackSequences([1,2,3,4,5], [4,5,3,2,1]));
console.log(validateStackSequences([1,2,3,4,5], [4,3,5,1,2]));